package tools;

import android.graphics.Bitmap;

import com.example.menhoo.toollibrary.common.utils.BitmapUtil;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import application.AppApplication;

/**
 * Created by yanglei on 2017/3/30.
 */

public class WeiXinTool {

    public static IWXAPI api;


    public static void text(String text){

        WXTextObject textObj = new WXTextObject();
        textObj.text=text;

        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject=textObj;
        msg.description=text;

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("text");
        req.message=msg;
        req.scene=SendMessageToWX.Req.WXSceneSession;
        if(AppApplication.getWeiXinApi()!=null){
            AppApplication.getWeiXinApi().sendReq(req);
        }
    }

    public static void html(String title, Bitmap thumb,String url,String desc){
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl=url;

        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject=webpage;
        msg.title=title;
        msg.description=desc;
        msg.thumbData= BitmapUtil.bitmapToByte(thumb);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.message=msg;
        req.scene=SendMessageToWX.Req.WXSceneSession;
        if(AppApplication.getWeiXinApi()!=null){
            AppApplication.getWeiXinApi().sendReq(req);
        }

    }

    /**
     * 构建一个唯一标志
     *
     * @param type 分享的类型分字符串
     * @return 返回唯一字符串
     */
    private static String buildTransaction(String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

}
